package com.fingard.dsp.bank.directbank.gzelink01;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;


/**
 * 广银联渠道余额查询接口
 * 
 * <p />参考文档：代收付系统接口2.5.2.doc
 * 
 * @author fanh
 * @date 2019/3/22
 */
public class GZELINK01Balance extends GZELINK01Base implements IBalance {
	/**
	 * 接口列表
	 * 文档章节   ->  6.1    账户余额查询
	 */
	@Override
	public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506)
			throws Exception {
		RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
		RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
		retAts.writeStart();

		try {
//			参照文档给接口赋值，通用的已被定义直接给值
			GZELINK01Header bankHeader = new GZELINK01Header();
			bankHeader.transCode = "500001";
			bankHeader.reqSeqNo = p_req7506.reqHeader.transSeqID;

			while (p_req7506.readRecord()) {
//				根据config中的配置拿到银行账号actNum再拿到对象actItem，
				ActSetItem actItem = getBankConfig().getActSetItem(p_req7506.readedDetail.actNum, "");
				StringBuilder p_reqMsg = new StringBuilder();
				writeCommonHeader(p_reqMsg, actItem, bankHeader);
				p_reqMsg.append("<ACCOUNT_TYPE>0</ACCOUNT_TYPE>");
				p_reqMsg.append("<MERCHANT_ID>" + actItem.merchantId + "</MERCHANT_ID>");
				p_reqMsg.append("<SUBMIT_TIME>" + Format.DateTime14Format.format(new Date()) +"</SUBMIT_TIME>");
				p_reqMsg.append("<QUERY_REMARK></QUERY_REMARK>");
				p_reqMsg.append("</TRANS_SUM>");
				p_reqMsg.append("</BODY>");
				p_reqMsg.append("</GZELINK>");

				// 签名
				String signData = p_reqMsg.toString().replaceAll("<SIGNED_MSG></SIGNED_MSG>", "");
				WriteBankLogLn2("广银联余额查询签名原始数据:\r\n" + signData);
				String signature = signMsg(signData, actItem);

				// 请求数据
				String reqData = p_reqMsg.toString().replaceAll("<SIGNED_MSG></SIGNED_MSG>","<SIGNED_MSG>" + signature + "</SIGNED_MSG>");
				WriteBankLogStep2(reqData);
				
//				拿到银行返回报文，用字符串数组接收
				String[] tmpStrRet = sendDataToGZE(reqData,bankFront.serverURL);
				WriteBankLogStep3(tmpStrRet[1]);
				
				if (tmpStrRet[0].length() == 0) {
					
//					如果返回的值为空,则说明发送成功,如果不成功则返回错误信息或者-1
					StringBuilder tmpSbMsg = new StringBuilder();
					XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
//					如果不为空,则交易码赋值给它0 
					if (tmpBkReader != null) {

						String tmpRespInfo = tmpBkReader.getFirstText("/GZELINK/INFO/ERR_MSG");
						String tmpRespCode = tmpBkReader.getFirstText("/GZELINK/INFO/RET_CODE");
//						0000状态表示处理完毕
						if ("0000".equals(tmpRespCode)) {
							if(tmpBkReader.read("/GZELINK/BODY/TRANS_SUM")){
								tmpRetDetail.respCode = "0";
								tmpRetDetail.respInfo = tmpRespInfo;
							 	tmpRetDetail.actArea = p_req7506.readedDetail.actArea;
                                tmpRetDetail.actCur = p_req7506.readedDetail.actCur;
                                tmpRetDetail.actNum = p_req7506.readedDetail.actNum;
                                tmpRetDetail.currBal = StringHelper.amountDivide100(tmpBkReader.readedXElement.getSingleText("BALANCE"));
								tmpRetDetail.frozenBal = StringHelper.amountDivide100(tmpBkReader.readedXElement.getSingleText("FORZEN_BALANCE"));
								tmpRetDetail.availBal = StringHelper.amountDivide100(tmpBkReader.readedXElement.getSingleText("AVAILABLE_BALANCE"));
							}
						} else {
							tmpRetDetail.respCode = "-1";
							tmpRetDetail.respInfo = tmpRespInfo;
						}
					} else {
						tmpRetDetail.respCode = "-1";
						tmpRetDetail.respInfo = tmpSbMsg.toString();
					}
				} else {
					tmpRetDetail.respCode = "-1";
					tmpRetDetail.respInfo = tmpStrRet[1];
				}
			} 
			retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
		}catch (Exception ex) {
			ex.printStackTrace();
			retAts.writeRespCode("-1");
			retAts.writeRespInfo(ex.getMessage());
			WriteBankLogLn(ex);
		}
//		补全标签
		retAts.writeEnd();
		return retAts;
	}

	@Override
	public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505)
			throws Exception {
		return null;
	}

}
